/*
    Geocache: some utilities for managing and visualizing geocache information
    Copyright (C) 2008  Gary Jackson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * PartToZipStreams.java
 */

package com.thegbomb.geocache;

import com.thegbomb.common.*;
import com.thegbomb.common.Mapper;
import com.thegbomb.common.NullIterator;
import com.thegbomb.common.SingleIterator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.zip.ZipInputStream;
import javax.mail.MessagingException;
import javax.mail.Part;
import org.apache.log4j.Logger;

/**
 *
 * @author Gary Jackson
 */
public class PartToZipStreams implements Mapper<Part, ZipInputStream> {
    static private Logger logger = Logger.getLogger(MessageToParts.class);
    public Iterator<ZipInputStream> map(Part input) {
        try {
            Object content = input.getContent();
            if (content instanceof InputStream)
                return new SingleIterator<ZipInputStream>(new ZipInputStream((InputStream)content));
            else
                return new NullIterator<ZipInputStream>();
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
